/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UDP;

import BittorentPackage.BittorentPackageMessage;
import common.function_common;
import java.io.*;
import java.net.*;

/**
 *
 * @author DongVan
 */
public class Sender {
    
    public Sender() {
    
    }
    
    public boolean sendData(int port, String ipAddress, BittorentPackageMessage pack) {   
        try {
            DatagramSocket socket = new DatagramSocket();
            //Gan dia chi IP va port vao InetAddress
            //InetAddress ipServer= InetAddress.getByName("localhost");
            InetAddress ipServer = InetAddress.getByName(ipAddress);
            //Goi thong diep cho Server
            byte[] sendData = function_common.serializeObject(pack);
            //Khoi tao DatagramPacket
            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, ipServer, port);
            //Send DatagramPacket
            socket.send(sendPacket);
            //dong DatagramSocket
            socket.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    
    
}
